package com.yachang.qym.usbhost.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.model.CallHelpModel;
import com.yachang.qym.usbhost.utils.API;
import com.yachang.qym.usbhost.utils.AllData;
import com.yachang.qym.usbhost.utils.LoadImage;
import com.yachang.qym.usbhost.utils.SendData;
import com.yachang.qym.usbhost.utils.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class HelpActivity extends AppCompatActivity {

    private CallHelpModel mCallHelpModel;
    private Toolbar mToolbar;
    private CircleImageView ivHead;
    private TextView tvName;
    private TextView tvHint;
    private TextView btnHelp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        mCallHelpModel = (CallHelpModel) getIntent().getSerializableExtra("model");
        initView();
        bindData();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView(){
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("搜救信息");
        ivHead = (CircleImageView) findViewById(R.id.iv_help_head);
        tvName = (TextView) findViewById(R.id.tv_help_name);
        tvHint = (TextView) findViewById(R.id.tv_help_hint);
        btnHelp = (TextView) findViewById(R.id.btn_help_him);
    }

    private void bindData(){
        tvName.setText(mCallHelpModel.getName()+"");
        tvHint.setText(mCallHelpModel.getHigh()+"");
        LoadImage.showImage(mCallHelpModel.getUserImage(), ivHead);
        btnHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendData.data = SendData.enterHelper(Integer.parseInt(AllData.machineId));
                SendData.needSend = true;
                mCallHelpModel.setHelperOid(AllData.outPolice_id);
                mCallHelpModel.setHelperId(AllData.userModel.get_id());
                mCallHelpModel.setHelperName(AllData.userModel.getUsername());
                mCallHelpModel.setHelperImage(AllData.userModel.getImageUrl());

                Map<String, Object> map = new HashMap<String, Object>();
                map.put("helperOid", AllData.outPolice_id);
                map.put("helperId", AllData.userModel.get_id());
                map.put("helperImage", AllData.userModel.getImageUrl());
                map.put("helperName", AllData.userModel.getUsername());
                map.put("uid", mCallHelpModel.getUid());
                map.put("mid", mCallHelpModel.getMid());
                map.put("oid", mCallHelpModel.getOid());
                map.put("high", mCallHelpModel.getHigh());
                map.put("fid", mCallHelpModel.getFid());
                map.put("name", mCallHelpModel.getName());
                map.put("image", mCallHelpModel.getUserImage());
                JSONObject jsonObject = new JSONObject(map);

                API.mSocket.emit("helperAdd", jsonObject);
                HelpActivity.this.finish();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                HelpActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
